﻿using Sasc.PV.BLL;
using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sasc.PV.Web
{
    public abstract class PVSelectDialogAdminPage : PVAdminPage
    {
        #region Fields
        private GridView _GVData;
        private Label _LabPageSum;
        private Label _LabCurrentPage;
        private TextBox _SearchTextBox;
        private TextBox _GoPage;
        private TextBox _TxtPageSize;
        #endregion

        #region InitializeControls
        protected void InitializeControls(GridView gView, Label pageSum, Label currentPage, TextBox pageSize, TextBox goPage, TextBox searchTextBox = null)
        {
            _GVData = gView;
            _LabPageSum = pageSum;
            _LabCurrentPage = currentPage;
            _TxtPageSize = pageSize;
            _GoPage = goPage;
            _SearchTextBox = searchTextBox;
        }
        #endregion

        #region Abstract
        protected abstract DataSet GetGridViewDataSource(int pageIndex, int pageSize, string searchText);
        #endregion

        #region Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBindToGridview();
            }
        }
        #endregion

        #region Bind Grid View
        protected virtual void DataBindToGridview()
        {
            _GVData.DataSource = GetGridViewDataSource(_GVData.PageIndex, _GVData.PageSize, _SearchTextBox != null ? _SearchTextBox.Text : string.Empty);
            _GVData.DataBind();
            _LabPageSum.Text = Convert.ToString(_GVData.PageCount);
            _LabCurrentPage.Text = Convert.ToString(((int)_GVData.PageIndex + 1));
            this._GoPage.Text = _LabCurrentPage.Text.ToString();
        }
        #endregion

        #region  Paging
        protected virtual void ButtonGo_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (_GoPage.Text.Trim().ToString() == "")
                {
                    Response.Write("<script language='javascript'>alert('页码不可以为空!');</script>");
                }
                else if (_GoPage.Text.Trim().ToString() == "0" || Convert.ToInt32(_GoPage.Text.Trim().ToString()) > _GVData.PageCount)
                {
                    Response.Write("<script language='javascript'>alert('页码不是一个有效值!');</script>");
                }
                else if (_GoPage.Text.Trim() != "")
                {
                    int PageI = Int32.Parse(_GoPage.Text.Trim()) - 1;
                    if (PageI >= 0 && PageI < (_GVData.PageCount))
                    {
                        _GVData.PageIndex = PageI;
                    }
                }

                if (_TxtPageSize.Text.Trim().ToString() == "")
                {
                    Response.Write("<script language='javascript'>alert('每页显示行数不可以为空!');</script>");
                }
                else if (_TxtPageSize.Text.Trim().ToString() == "0")
                {
                    Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
                }
                else if (_TxtPageSize.Text.Trim() != "")
                {
                    try
                    {
                        int MyPageSize = int.Parse(_TxtPageSize.Text.ToString().Trim());
                        this._GVData.PageSize = MyPageSize;
                    }
                    catch
                    {
                        Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
                    }
                }

                DataBindToGridview();
            }
            catch
            {
                DataBindToGridview();
                Response.Write("<script language='javascript'>alert('请输入有效数字！');</script>");
            }
        }

        protected void PagerButtonClick(object sender, ImageClickEventArgs e)
        {
            //获得Button的参数值
            string arg = ((ImageButton)sender).CommandName.ToString();
            switch (arg)
            {
                case ("Next"):
                    if (this._GVData.PageIndex < (_GVData.PageCount - 1))
                        _GVData.PageIndex++;
                    break;
                case ("Pre"):
                    if (_GVData.PageIndex > 0)
                        _GVData.PageIndex--;
                    break;
                case ("Last"):
                    try
                    {
                        _GVData.PageIndex = (_GVData.PageCount - 1);
                    }
                    catch
                    {
                        _GVData.PageIndex = 0;
                    }

                    break;
                default:
                    //本页值
                    _GVData.PageIndex = 0;
                    break;
            }
            DataBindToGridview();
        }
        #endregion

        #region Grid View RowDataBound
        protected virtual void GVData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            PublicMethod.GridViewRowDataBound(e);
        }
        #endregion
    }
}